package com.library.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.common.utils.CommonUtil;
import com.library.entity.InfoSms;
import com.library.mapper.SmsMapper;
import com.library.service.SmsService;
import com.library.util.SmsUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dhing
 * @since 2020-12-28
 */
@Service
public class SmsServiceImpl extends ServiceImpl<SmsMapper, InfoSms> implements SmsService {

	@Override
	public boolean sendSms(String phone) {
		InfoSms sms = new InfoSms();
		// 生成验证码
		String code = CommonUtil.getCode();
		sms.setCode(code);
		sms.setMobile(phone);
		// 发送短信
		boolean bool = SmsUtil.sendSms(phone, "SMS_201565033", JSON.toJSONString(sms));

		if (bool) {
			sms.setRemark("success");
			// 发送成功保存数据
			return save(sms);
		}
		return false;
	}


	@Override
	public boolean sendSms(String phone, String code) {
		InfoSms sms = new InfoSms();
		// 生成验证码
		sms.setCode(code);
		sms.setMobile(phone);
		// 发送短信
		boolean bool = SmsUtil.sendSms(phone, "SMS_201565033", JSON.toJSONString(sms));

		if (bool) {
			sms.setRemark("success");
			// 发送成功保存数据
			sms.setCreateTime(LocalDateTime.now());
			return save(sms);
		}
		return false;
	}


	@Override
	public boolean sendSms(String phone, String code, String tmpCode) {
		InfoSms sms = new InfoSms();
		// 生成验证码
		sms.setCode(code);
		sms.setMobile(phone);
		// 发送短信
		boolean bool = SmsUtil.sendSms(phone, tmpCode, JSON.toJSONString(sms));

		if (bool) {
			sms.setRemark("success");
			// 发送成功保存数据
			return save(sms);
		}
		return false;
	}

	@Override
	public boolean sendSmsInvite(String phone) {
		InfoSms sms = new InfoSms();
		// 生成验证码
		sms.setMobile(phone);
		// 发送短信
		boolean bool = SmsUtil.sendSms(phone, "SMS_201565033", JSON.toJSONString(sms));

		if (bool) {
			sms.setRemark("success");
			// 发送成功保存数据
			sms.setCreateTime(LocalDateTime.now());
			return save(sms);
		}
		return false;
	}

}
